package ru.teleology.cubefs.api.reflection;

public class LibraryLoader {
	
	public static final int GLOBAL 	= 1; 
	public static final int LOCAL 	= 2;
	
	static {
		System.load("/home/victor/workspace1/core-java-native/build/loader/libcubefs-java-loader.so");
	}
	
	public static long load(String name, int mode){
		long handle = openLib(name, mode);
		if (handle != 0) {
			return handle;
		}
		else {
			throw new UnsatisfiedLinkError(getError());
		}
	}
	
	public static void close(long handle){
		if (closeLib(handle) != 0) {
			throw new UnsatisfiedLinkError(getError());
		}
	}
	
	public static long sym(long handle, String name){
		long shandle = getSym(handle, name);
		if (shandle != 0) {
			return handle;
		}
		else {
			throw new UnsatisfiedLinkError(getError());
		}
	}
	
	private static native long openLib(String name, int mode); 
	private static native int  closeLib(long handle);
	private static native long getSym(long handle, String name);
	private static native String getError();
	
	private static native void deleteGlobalRef(Object obj);
}
